//
//  ZTXBangDingBackCodeView.m
//  NtoccDriverNew
//
//  Created by 张天兴 on 2024/8/29.
//  Copyright © 2024 GCY. All rights reserved.
//

#import "ZTXBangDingBackCodeView.h"

@implementation ZTXBangDingBackCodeView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUI];
    }
    return self;
}
- (void)setUI{
    [self addSubview:self.bottomBgView];
}
- (UIView *)bottomBgView{
    if (!_bottomBgView) {
        self.bottomBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        self.bottomBgView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
        [self.bottomBgView addSubview:self.bgView];
    }
    return _bottomBgView;
}
- (UIView *)bgView{
    if (!_bgView) {
        self.bgView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - SafeAreaBottomHeight - 306, SCREEN_WIDTH, 306)];
        self.bgView.backgroundColor = CJRGBColor(245, 248, 254);
        self.bgView.layer.masksToBounds = YES;
        self.bgView.layer.cornerRadius = 10;
        [self.bgView addSubview:self.bodyTitleLabel];
        [self.bgView addSubview:self.phoneNameLabel];
        [self.bgView addSubview:self.phoneTextLabel];
        [self.bgView addSubview:self.codeNameLabel];
        [self.bgView addSubview:self.codeTextField];
        [self.bgView addSubview:self.codeButton];
        [self.bgView addSubview:self.commintButton];
    }
    return _bgView;
}
-(UILabel *)bodyTitleLabel{
    if (!_bodyTitleLabel) {
        self.bodyTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, SCREEN_WIDTH - 20, 30)];
        self.bodyTitleLabel.text = @"鉴权验证码验证";
        self.bodyTitleLabel.font = [UIFont systemFontOfSize:18];
        self.bodyTitleLabel.textColor = [UIColor whiteColor];
        self.bodyTitleLabel.textAlignment = NSTextAlignmentCenter;
        self.cententButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        self.cententButton.frame = CGRectMake(SCREEN_WIDTH - 10 - 50, 7, 32, 32);
        [self.cententButton setBackgroundImage:[UIImage imageNamed:@"Instagram"] forState:(UIControlStateNormal)];
        [self.cententButton addTarget:self action:@selector(cententButtonAction) forControlEvents:(UIControlEventTouchUpInside)];
        [self.bgView addSubview:self.cententButton];
    }
    return _bodyTitleLabel;
}
-(UILabel *)phoneNameLabel{
    if (!_phoneNameLabel) {
        self.phoneNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(self.bodyTitleLabel.frame) + 15, 70, 30)];
        self.phoneNameLabel.text = @"手机号";
        self.phoneNameLabel.font = [UIFont systemFontOfSize:15];
        self.phoneNameLabel.textColor = [UIColor whiteColor];
    }
    return _phoneNameLabel;
}
-(UILabel *)phoneTextLabel{
    if (!_phoneTextLabel) {
        self.phoneTextLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.phoneNameLabel.frame) + 10, CGRectGetMaxY(self.bodyTitleLabel.frame) + 15, 120, 30)];
        self.phoneTextLabel.text = self.phoneStr;
        self.phoneTextLabel.font = [UIFont systemFontOfSize:15];
//        self.phoneTextLabel.textColor = Light_TextColor;
        self.phoneTextLabel.layer.masksToBounds = YES;
        self.phoneTextLabel.layer.borderWidth = 1;
//        self.phoneTextLabel.layer.borderColor = Line_TextColor.CGColor;
//        self.phoneTextLabel.backgroundColor = Line_TextColor;
        self.phoneTextLabel.layer.cornerRadius = 5;
    }
    return _phoneTextLabel;
}
- (UIButton *)codeButton{
    if (!_codeButton) {
        self.codeButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
//        self.codeButton.backgroundColor = Theme_ColorValue;
        [self.codeButton setTitle:@"发送验证码" forState:(UIControlStateNormal)];
        self.codeButton.layer.masksToBounds = YES;
        self.codeButton.layer.cornerRadius = 10;
//        self.codeButton.frame = CGRectMake(ScreenW - 130, CGRectGetMaxY(self.bodyTitleLabel.frame) + 5, 120, 40);
        [self.codeButton setTag:102];
        [self.codeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self.codeButton addTarget:self action:@selector(codeButtonAction) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _codeButton;
}
- (UILabel *)codeNameLabel{
    if (!_codeNameLabel) {
        self.codeNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(self.codeButton.frame) + 15, 70, 30)];
        self.codeNameLabel.text = @"验证码";
        self.codeNameLabel.font = [UIFont systemFontOfSize:15];
//        self.codeNameLabel.textColor = Dark_TextColor;
    }
    return _codeNameLabel;
}
- (UITextField *)codeTextField{
    if (!_codeTextField) {
//        self.codeTextField = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.codeNameLabel.frame) + 10, CGRectGetMaxY(self.codeButton.frame) + 15, ScreenW -  120, 40)];
        self.codeTextField.placeholder = @"请输入鉴权验证码";
        self.codeTextField.keyboardType = UIKeyboardTypeNumberPad;
        self.codeTextField.layer.masksToBounds = YES;
        self.codeTextField.layer.cornerRadius = 10;
        self.codeTextField.layer.borderWidth = 1;
//        self.codeTextField.layer.borderColor = Line_TextColor.CGColor;
        [self.codeTextField setValue:[NSNumber numberWithInt:10] forKey:@"paddingLeft"];

        
    }
    return _codeTextField;
}
- (UIButton *)commintButton{
    if (!_commintButton) {
        self.commintButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
//        self.commintButton.backgroundColor = Theme_ColorValue;
        [self.commintButton setTitle:@"确 认 绑 卡" forState:(UIControlStateNormal)];
        self.commintButton.layer.masksToBounds = YES;
        self.commintButton.layer.cornerRadius = 10;
//        self.commintButton.frame = CGRectMake(10, CGRectGetMaxY(self.codeTextField.frame) + 20, ScreenW - 20, 44);
        [self.commintButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        [self.commintButton addTarget:self action:@selector(commintButtonAction) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _commintButton;
}
- (void)codeButtonAction{
    [self endEditing:YES];
    self.blockJianQuanCode();
    [self theCountdown];
    
}
- (void)commintButtonAction{
    if (self.codeTextField.text.length <= 0) {
//        [XSInfoView showInfo:@"请输入验证码" onView:self];
        return;
    }
    self.blockCommintCode(self.codeTextField.text);
}
- (void)cententButtonAction{
    self.blockCententButton();
}
- (void)theCountdown{
    
    UIButton *button = [self viewWithTag:102];
    
    __block int timeout= 59; //倒计时时间
    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    
    dispatch_source_set_event_handler(_timer, ^{
        
        if(timeout<=0){ //倒计时结束，关闭
            
            dispatch_source_cancel(_timer);
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //设置界面的按钮显示 根据自己需求设置
                
                [button setTitle:@"发送验证码" forState:UIControlStateNormal];
                [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//                [button setBackgroundColor:Theme_ColorValue];
                button.userInteractionEnabled = YES;
            });
            
        }else{
            
            //            int minutes = timeout / 60;
            
            int seconds = timeout % 60;
            
            NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //设置界面的按钮显示 根据自己需求设置
                
                [UIView beginAnimations:nil context:nil];
                
                [UIView setAnimationDuration:0];
                
                [button setTitle:[NSString stringWithFormat:@"%@ S",strTime] forState:UIControlStateNormal];
                //                [button setBackgroundColor:[UIColor whiteColor]];
                [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                //                cutBorder(_sendCodeBtn, .5f, @"999999");
                //                [button setBackgroundColor:[UIColor clearColor]];
                [UIView commitAnimations];
                
                button.userInteractionEnabled = NO;
                
            });
            
            timeout--;
            
        }
        
    });
    
    dispatch_resume(_timer);
}
- (void)upDataPhone:(NSString *)phone{
    self.phoneTextLabel.text = phone;
}
@end
